<?php
App::uses('AppController', 'Controller');
/**
 * StoryItems Controller
 *
 * @property StoryItem $StoryItem
 * @property PaginatorComponent $Paginator
 */
class StoryItemsController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator');

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->StoryItem->recursive = 0;
		$this->set('storyItems', $this->Paginator->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->StoryItem->exists($id)) {
			throw new NotFoundException(__('Invalid story item'));
		}
		$options = array('conditions' => array('StoryItem.' . $this->StoryItem->primaryKey => $id));
		$this->set('storyItem', $this->StoryItem->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add($sid = null) {
	
		//TODO - Get uid from Session
		$uid = 7;
		if ($this->request->is('post')) {
			
			//Get sid from session
			$sid = $this->Session->read('sid');
			$this->StoryItem->create();
			$this->StoryItem->set($this->request->data);
			$this->StoryItem->set(array('story_id'	=>	$sid,
										'status'	=>	StoryItemsController::STORY_ITEM_STATUS_OK));
										
			if ($this->StoryItem->save()) {
				$this->Session->setFlash(__('The story item has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The story item could not be saved. Please, try again.'));
			}
		}
		
		//leave story id in session
		$this->Session->write('sid', $sid);
		$stories = $this->StoryItem->Story->find('list');
		$users = $this->StoryItem->User->find('list');
		$this->set(compact('stories', 'users'));
		$this->set(compact('sid', $sid));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->StoryItem->exists($id)) {
			throw new NotFoundException(__('Invalid story item'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if ($this->StoryItem->save($this->request->data)) {
				$this->Session->setFlash(__('The story item has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The story item could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('StoryItem.' . $this->StoryItem->primaryKey => $id));
			$this->request->data = $this->StoryItem->find('first', $options);
		}
		$stories = $this->StoryItem->Story->find('list');
		$users = $this->StoryItem->User->find('list');
		$this->set(compact('stories', 'users'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->StoryItem->id = $id;
		if (!$this->StoryItem->exists()) {
			throw new NotFoundException(__('Invalid story item'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->StoryItem->delete()) {
			$this->Session->setFlash(__('The story item has been deleted.'));
		} else {
			$this->Session->setFlash(__('The story item could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'index'));
	}
	
	public function adopt($id = null){
		$this->StoryItem->id = $id;
		if (!$this->StoryItem->exists()) {
			throw new NotFoundException(__('Invalid story item'));
		}
		$this->request->onlyAllow('post', 'adopt');
		
		//Check if item already adopted
		$item = $this->StoryItem->find('first', array('conditions' => array('StoryItem.id' => $id)));
		if ($item['StoryItem']['adopted']){
			$this->Session->setFlash(__(StoryItemsController::STORY_ITEM_MESSAGES_ALREADT_ADOPTED));
			return $this->redirect(array('action' => 'index'));
		}
		
		//Get user id from Session
		$uid = 1;
		$this->doAdoption($id, true, $uid);
		return $this->redirect(array('action' => 'index'));
	}
	
	public function unadopt($id = null){
		$this->StoryItem->id = $id;
		if (!$this->StoryItem->exists()) {
			throw new NotFoundException(__('Invalid story item'));
		}
		$this->request->onlyAllow('post', 'unadopt');
		
		//Check if current user is the adopter
		$item = $this->StoryItem->find('first', array('conditions' => array('StoryItem.id' => $id)));
		//Get user id from Session
		$uid = 1;
		if ($item['StoryItem']['user_id'] != $uid){
			$this->Session->setFlash(__(StoryItemsController::STORY_ITEM_MESSAGES_U_CANNOT_UNADOPT));
			return $this->redirect(array('action' => 'index'));
		}
		
		$this->doAdoption($id, false, $uid);
		return $this->redirect(array('action' => 'index'));
	}
	
	/*****
		Change the adopted field of the item
	*****/
	private function doAdoption($item_id, $adopt, $uid){
		$user_id = $adopt ? $uid : 0;
		$this->StoryItem->set(array('user_id' => $uid, 'adopted' => $adopt));
		$this->StoryItem->save();
		
		$msg = $adopt ? StoryItemsController::STORY_ITEM_MESSAGES_ADOPTED : StoryItemsController::STORY_ITEM_MESSAGES_UNADOPTED;
		$this->Session->setFlash(__($msg));
	}
	
	const STORY_ITEM_MESSAGES_ADOPTED				= "Item has been adopted, thank you.";
	const STORY_ITEM_MESSAGES_UNADOPTED				= "Item has been returned to pool...";
	const STORY_ITEM_MESSAGES_ALREADT_ADOPTED		= "Item has already been adopted, Sorry.";
	const STORY_ITEM_MESSAGES_U_CANNOT_UNADOPT 		= "You are not the Adopter of this Item, Unadoption rejected!";
	
	const STORY_ITEM_STATUS_OK 							= 1;
	const STORY_ITEM_STATUS_PENDING_ADOPTION_APPROVAL	= 2;
	const STORY_ITEM_STATUS_ADOPTED						= 3;
}